/**
 * 
 */
package com.virxue.baseweb.interceptors;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

/**
 * @ClassName LoggerInterceptor
 * @Description 日志拦截器
 *
 * @author sunhaojie 3113751575@qq.com
 * @date 2015年11月22日 下午9:01:01
 */
public class LoggerInterceptor implements HandlerInterceptor {

	private static Logger logger = LoggerFactory.getLogger(LoggerInterceptor.class);

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	@SuppressWarnings("rawtypes")
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String pathInfo = request.getPathInfo();
		Map parameterMap = request.getParameterMap();
		String paramString = JSONObject.toJSONString(parameterMap);
		logger.info("begin: {} request params {}", pathInfo, paramString);
		return true;
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
	 */
	@SuppressWarnings("rawtypes")
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		String pathInfo = request.getPathInfo();
		Map parameterMap = request.getParameterMap();
		String paramString = JSONObject.toJSONString(parameterMap);
		logger.info("end: {} request params {}", pathInfo, paramString);
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
	 */
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
